package com.coocaa.ops.admin.v1.panel.validator;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.coocaa.ops.admin.base.exception.AdminBizException;
import com.coocaa.ops.admin.base.validator.InputParamsValidator;
import com.coocaa.ops.admin.v1.panel.constant.AiIconConstants;
import com.coocaa.ops.common.core.base.util.ResourceStatusValidator;
import com.coocaa.ops.common.core.biz.panel.constant.PanelTypeConstants;
import com.coocaa.ops.common.core.biz.panel.entity.BlockContentConfigInfo;
import com.coocaa.ops.common.core.biz.panel.entity.PanelInfoEntity;
import com.coocaa.ops.common.core.biz.panel.entity.VirtuallyBlockInfo;
import lombok.experimental.UtilityClass;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;

import java.util.List;

/**
 * @author bijiahao
 * @description
 * @date 2020/6/18 10:30
 */
@UtilityClass
public class PanelValidator {
    /**
     * 保存前的校验
     *woxiangxiaban
     * @param entity
     */
    public static void validBeforeSave(PanelInfoEntity entity) {
        if (ResourceStatusValidator.isOnline(entity.getStatus())) {
            throw new AdminBizException("审核通过的不能编辑");
        }
        Assert.hasText(entity.getPanelType(), "板块类型能不能为空");
        if (!PanelTypeConstants.isMark(entity.getPanelType())) {
            Assert.notNull(entity.getBusinessType(), "业务类型不能为空");
        }
        Assert.hasText(entity.getPanelName(), "板块名称不能为空");
        if (PanelTypeConstants.isNormal(entity.getPanelType())) {
            Assert.notEmpty(entity.getBlockList(), "关联推荐位信息不能为空");
            validRlsBlock(entity.getBlockList(), entity.getStatus());
        }
        if (PanelTypeConstants.isGroup(entity.getPanelType())) {
            Assert.notEmpty(entity.getSubPanelList(), "子版块信息不能为空");
        }
        if (PanelTypeConstants.isMark(entity.getPanelType())) {
            Assert.hasText(entity.getClientType(), "客户端类型不能为空");
        } else if (PanelTypeConstants.isPlugin(entity.getPanelType())) {
            Assert.notNull(entity.getPluginInfo(), "插件信息不能为空");
        } else if (PanelTypeConstants.isRecStream(entity.getPanelType())) {
            //空
        } else {
            Assert.notNull(entity.getLayoutId(), "布局ID不能为空");
        }
    }

    /**
     * 校验点击事件
     *
     * @param onclick
     */
    public static void checkOnclick(String onclick) {
        Assert.hasText(onclick, "点击事件不能为空");
        JSONObject onclickJsonObj = JSON.parseObject(onclick);
        String packagename = onclickJsonObj.getString("packagename");
        Assert.hasText(packagename, "点击事件包名为空: " + onclick);
    }

    public static boolean isOnclickEmpty(String onclick) {
        if (StringUtils.isBlank(onclick)) {
            return true;
        }
        JSONObject onclickJsonObj = JSON.parseObject(onclick);
        String packagename = onclickJsonObj.getString("packagename");
        return StringUtils.isBlank(packagename);
    }

    public static boolean isPanelHasDmp(List<VirtuallyBlockInfo> blockList) {
        for (VirtuallyBlockInfo virtuallyBlockInfo : blockList) {
            List<BlockContentConfigInfo> dmpBlockList = virtuallyBlockInfo.getDmpBlockList();
            if (isHasDmp(dmpBlockList)) {
                return true;
            }
        }
        return false;
    }


    private static void validRlsBlock(List<VirtuallyBlockInfo> blockList, Integer status) {
        if (ResourceStatusValidator.isToAuditing(status)) {
            //待审核的进行校验
            for (VirtuallyBlockInfo virtuallyBlockInfo : blockList) {
                List<BlockContentConfigInfo> commonBlockList = virtuallyBlockInfo.getCommonBlockList();
                Assert.notEmpty(commonBlockList, "通用内容块信息不能为空");
                validBlockContentList(commonBlockList, false);
                List<BlockContentConfigInfo> dmpBlockList = virtuallyBlockInfo.getDmpBlockList();
                validBlockContentList(dmpBlockList, true);

            }
        }
    }

    private static void validBlockContentList(List<BlockContentConfigInfo> blockContentConfigInfoList, boolean isDmp) {
        if (CollectionUtils.isEmpty(blockContentConfigInfoList)) {
            return;
        }
        for (BlockContentConfigInfo blockContentConfigInfo : blockContentConfigInfoList) {
            validBlockContent(blockContentConfigInfo, isDmp);
        }
    }

    private void validBlockContent(BlockContentConfigInfo blockContentConfigInfo, boolean isDmp) {
        BlockValidator.valid(blockContentConfigInfo, isDmp);
        String finalIconTextPositionsJsonStr = AiIconConstants.getFinalIconTextPositionsJsonStr(blockContentConfigInfo.getIconTextPositionsJsonStr());
        BlockValidator.validIconText(finalIconTextPositionsJsonStr);
        blockContentConfigInfo.setIconTextPositionsJsonStr(finalIconTextPositionsJsonStr);
    }


    private static boolean isHasDmp(List<BlockContentConfigInfo> dmpBlockList) {
        if (CollectionUtils.isEmpty(dmpBlockList)) {
            return false;
        }
        for (BlockContentConfigInfo blockContentConfigInfo : dmpBlockList) {
            if (InputParamsValidator.isDmpContentAvailable(blockContentConfigInfo.getDmpRegistryInfo())) {
                return true;
            }
        }
        return false;
    }
}
